package com.potato.dashboard.web.controller;



import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.analyze.datasource.mgr.DataSourceMgr;
import com.potato.dashboard.dao.ChartTemplateDao;
import com.potato.dashboard.model.ChartTemplate;

@Controller
public class DashboardController {
	@Autowired
	private ChartTemplateDao chartTemplateDao;
	@Autowired
	private DataSourceMgr dataSourceMgr;
	
	@RequestMapping("/show.do")
	public ModelAndView show(Long id) {
		ChartTemplate template = chartTemplateDao.findById(id);
		return new ModelAndView("/analyze/show", "template", template);
	}
	
	@RequestMapping("/create.do")
	public ModelAndView create() {
		ChartTemplate template = new ChartTemplate();
		return new ModelAndView("/analyze/addTemplet");
	}

	@RequestMapping("/save.do")
	public String save(ChartTemplate template) {
		chartTemplateDao.save(template);
		return "redirect:/list.do";
	}

	@RequestMapping("/edit.do")
	public ModelAndView edit(Long id) {
		ChartTemplate template = chartTemplateDao.findById(id);
		return new ModelAndView("/analyze/edit", "template", template);
	}

	@RequestMapping("/update.do")
	public String update(ChartTemplate template) {
		chartTemplateDao.save(template);
		return "redirect:/list.do";
	}
	
	@RequestMapping("/delete.do")
	public String delete(Long id) {
		ChartTemplate template = chartTemplateDao.findById(id);
		if (template != null)
			chartTemplateDao.remove(template);
		return "redirect:/list.do";
	}
	
	@RequestMapping("/addChart.do")
	public ModelAndView addChart(){
		Map<String, List> map = new HashMap();	
		map.put("templets", chartTemplateDao.findAll());
		map.put("datasources", dataSourceMgr.getAllDataSource());
		return new ModelAndView("/analyze/addChart","map",map);
		
	}
	
	@RequestMapping("/doAddChart.do")
	public String doAddChart(HttpServletRequest request){
		
		return "redirect:/listChart.do";
	}
}
